package vy.org.app.page.printlabel.report.schedule;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDAO;
import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDO;
import org.vy.dal.dao.VyPrintLabelReportDAO;
import org.vy.dal.dao.VyPrintLabelReportDO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDAO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDAO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDO;

import vy.org.app.common.BaseAction;
import vy.org.app.common.ModuleEnum;
import vy.org.app.page.lookup.patrika.MntnPatrikaPeriodForm;
import vy.org.app.page.lookup.patrika.PatrikaHelper;

public class MntnPrintLabelReportScheduleAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		
		MntnPrintLabelReportScheduleForm form = (MntnPrintLabelReportScheduleForm)actionForm;
		if(form == null){
			form = new MntnPrintLabelReportScheduleForm();
		}
		request.setAttribute("mntnPrintLabelReportScheduleForm", form);
		
		try{
			String ops = request.getParameter("ops");
			if("EDIT".equals(ops) && form.getReportId() != null && form.getAck() == null){
				VyPrintLabelReportDO dto = VyPrintLabelReportDAO.findByReportId(Integer.parseInt(form.getReportId()));
				form = PrintLabelReportHelper.fillReportForm(dto, form);
			}
			List<VyLookupPatrikaDO> patrikaLst = VyLookupPatrikaDAO.findAllActive();
			List<ViewVyLookupPatrikaPeriodDO> patPeriodLst = ViewVyLookupPatrikaPeriodDAO.findAllActive();
			Map<String, List<MntnPatrikaPeriodForm>> patrikaMap = new HashMap<String, List<MntnPatrikaPeriodForm>>(); 
			
			for(VyLookupPatrikaDO pat : patrikaLst){
				patrikaMap.put(pat.getPatrikaId().toString(), PatrikaHelper.fillPatrikaPeriodForm(pat, patPeriodLst));
			}
			form.setPatrikas(patrikaMap);

			List<ViewVyLookupCitiesDO> countryLst = ViewVyLookupCitiesDAO.findUniqueCountries();
			List<ViewVyLookupCitiesDO> stateLst=null;
			List<ViewVyLookupCitiesDO> cityLst=null;
			if(form.getCountry() != null && !"".equals(form.getCountry()))
				stateLst = ViewVyLookupCitiesDAO.findStateByCountry(form.getCountry());
			if(form.getCountry() != null && !"".equals(form.getCountry())
					&& form.getStateOrRegion() != null && !"".equals(form.getStateOrRegion()))
				cityLst = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(form.getStateOrRegion(),form.getCountry());
			form.setCountries(countryLst);
			form.setStates(stateLst);
			form.setCities(cityLst);

			List<VyPrintLabelReportDO> reports = VyPrintLabelReportDAO.findAll();
			form.setReports(reports);
			form.setAck(null);

		}catch(Exception e){
			e.printStackTrace();
		}
		return mapping.findForward("success");
	}

	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.EDIT_SUBSCRIPTION;
	}

}
